export type MemoryState = 'pending' | 'accepted' | 'rejected';

export interface MemoryItem {
  id: string;
  content: string;
  state: MemoryState;
  version?: string;
  requestId?: string;
  timestamp: number;
}

export interface MemoryStore {
  listByState(state: MemoryState): Promise<MemoryItem[]> | MemoryItem[];
  updateState(id: string, state: MemoryState, editedContent?: string): Promise<void> | void;
}

export class InMemoryMemoryStore implements MemoryStore {
  private items = new Map<string, MemoryItem>();

  constructor(initial?: MemoryItem[]) {
    initial?.forEach(i => this.items.set(i.id, i));
  }

  listByState(state: MemoryState): MemoryItem[] {
    return [...this.items.values()].filter(i => i.state === state);
  }

  updateState(id: string, state: MemoryState, editedContent?: string): void {
    const it = this.items.get(id);
    if (!it) return;
    this.items.set(id, { ...it, state, content: editedContent ?? it.content });
  }

  upsert(item: MemoryItem) {
    this.items.set(item.id, item);
  }
}

